<?php
//地址信息
class App_Address extends Data_Base
{
	//默认地址
    public static function defaultAddr ($user, $addressId = NULL)
    {
        if ($addressId > 0)
            return self::detail($addressId);

        $addrs = self::infoList($user);
        if (! empty($addrs))
            return $addrs[0];
        else
            return NULL;
    }

	//设置默认地址
    public static function setDefault($user, $nid)
    {
        $rows = DB::update('user_v2_address')
            ->set(array('isDefault' => 0))
            ->where('uid', '=', $user->getUserUid())
            ->execute();

        $rows = DB::update('user_v2_address')
            ->set(array('isDefault' => 1))
            ->where('uid', '=', $user->getUserUid())
            ->where('id', '=', $nid)
            ->execute();
    }

	//地址详细信息
	//$id int 地址id
    public static function detail ($id) 
    {
        $rs = self::item('user_v2_address', 'id', $id);
        $rs['proviceName'] = App_Region::name($rs['proviceId']);
        $rs['cityName']    = App_Region::name($rs['cityId']);
        $rs['countryName'] = App_Region::name($rs['countryId']);

        return $rs;
    }

	//地址详细信息
	//$user 用户对象
    public static function infoList ($user)
    {
        $uid = $user->getUserUid();
        if ($uid == 0)
            return array();

        $query = DB::select()
            ->from('user_v2_address')
            ->where('uid', '=', $uid)
            ->order_by('isDefault', 'desc')
            ->order_by('changed', 'desc')
            ->execute();

        $ret = array();
        foreach ($query as $rs)
        {
            $rs['proviceName'] = App_Region::name($rs['proviceId']);
            $rs['cityName']    = App_Region::name($rs['cityId']);
            $rs['countryName'] = App_Region::name($rs['countryId']);

            if ($query->count() == 1)
                $rs['isDefault'] = 1;

            $ret[] = $rs;
        }

        return $ret;
    }

	//编辑地址
	//$user	用户对象 
	//$nid	地址编号
	//$data 地址信息
    public static function edit ($user, $nid, $data)
    {
        $row = self::buildDataFromRawInput($data);

        unset($row['id']);
        unset($row['uid']);
        unset($row['created']);
        $row['changed'] = DB::expr('unix_timestamp(now())');

        $rows = DB::update('user_v2_address')
            ->set($row)
            ->where('uid', '=', $user->getUserUid())
            ->where('id', '=', $nid)
            ->execute();

        return $rows > 0;
    }

	//添加地址信息
	//$user		用户对象
	//$data		地址信息
    public static function add ($user, $data)
    {
        $row = self::buildDataFromRawInput($data);

        unset($row['id']);
        $row['uid'] = $user->getUserUid();
        $row['created'] = DB::expr('unix_timestamp(now())');
        $row['changed'] = DB::expr('unix_timestamp(now())');

        list($insert_id, $rows) = DB::insert('user_v2_address')
            ->columns (array_keys($row))
            ->values (array_values($row))
            ->execute();

        return $insert_id;
    }

	//删除地址
	//$user	用户对象
	//$nid	地址编号
    public static function remove($user, $nid)
    {
        $rows = DB::delete('user_v2_address')
            ->where('uid', '=', $user->getUserUid())
            ->where('id', '=', $nid)
            ->limit(1)
            ->execute();

        return $rows > 0;
    }

	//构建地址信息
    protected static function buildDataFromRawInput ($data)
    {
        $ret = array();
        $ret['userName']   = (String) Arr::get($data, 'userName');
        $ret['telNumber']  = (String) Arr::get($data, 'telNumber');
        $ret['postalCode'] = (String) Arr::get($data, 'postalCode');
        $ret['proviceId']  = (int) Arr::get($data, 'proviceId');
        $ret['cityId']     = (int) Arr::get($data, 'cityId');
        $ret['countryId']  = (int) Arr::get($data, 'countryId');
        $ret['detailInfo'] = (String) Arr::get($data, 'detailInfo');

        return $ret;
    }
}
